package BNS.plumbing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;


/** This is a utility for creating ColoringGames and saving them in files.
	It is intended to get all the details and opportunities for error done correctly
	so that there is no load on the supervisor's brain cells during a live session.
	*/
public class BuildGames {
	private static String prep, arch;
	
	/** method to invoke to start the game building GUI and process.
		@param args holds the name of one file, from which all games and args are read.
		*/
	public static void main(String[] args) {
		prep= System.getProperty("PREP");
		if (prep==null) prep= "./";
		Library.setPrep(new File(prep));
		/* We discover the value of the ARCH property, which will be used
		in the Library for storing session files and exp files. */
		arch= System.getProperty("ARCH");
		if (arch==null) arch= "./output";
		Library.setArch(new File(arch));
		
		try {
			FileReader frdr= new FileReader(args[0]);
			System.err.println(" GameBuilder reading: "+args[0]);
			BufferedReader rdr= new BufferedReader(frdr);
			String line= rdr.readLine(); 
			while(line.length()>0) {
				System.err.println("read line: "+ line);
				StringTokenizer st= new StringTokenizer(line);
				buildGame(st);
				System.err.println("finished building game");
				System.err.println();
				line= rdr.readLine(); 
				}
		} catch(Exception e) { System.err.println(e); System.exit(99); }
		}
	
	private static void buildGame(StringTokenizer st) {
		String type= st.nextToken();
		String gfld= type +"-"+ st.nextToken() + ".game";
		BNSGameController game= null;
		if (type.equals("CLR")) game= BNS.coloring.ColoringGame.build(prep,st);
		if (type.equals("CNS")) game= BNS.coloring.ConsensusGame.build(prep,st);
		if (type.equals("VTR")) game= BNS.coloring.VoterGame.build(prep,st);
		if (game!=null) {
			System.err.println("game obtained: "+ game.getInfo());
			Library.putGame(new File(gfld),game);
			System.err.println("saved game at "+ gfld);
			}
		else System.err.println("FAILED to construct game "+gfld);
		}
		

	public static float[][] readRaggedArray(File fn) throws IOException {
		Vector<float[]> raggarray= new Vector<float[]>();
		System.err.println(" created "+raggarray.size()+" lines");
//		try {
			FileReader frdr= new FileReader(fn);
			System.err.println(" raggedArray reading: "+fn);
			BufferedReader rdr= new BufferedReader(frdr);
			String line= rdr.readLine(); 
			while(line!=null && line.length()>0) {
				StringTokenizer st= new StringTokenizer(line);
				int i=0, size= st.countTokens();
				float[] nmbrs= new float[size];
				System.err.print(line+" will yield "+nmbrs.length+" values:");
				while (st.hasMoreTokens())
					nmbrs[i++]= Float.parseFloat(st.nextToken());
				raggarray.add(nmbrs);
				System.err.println(" "+nmbrs.length);
				line= rdr.readLine(); 
				}
//		} catch(Exception e) { System.err.println(e); System.exit(99); }
		System.err.println(" found "+raggarray.size()+" lines");
		float[][] vecs= new float[2][];
		return raggarray.toArray(vecs);
		}
		
	} // EOClass GameBuilder
